<template>
  <t-dialog
    header="专家短信邀请模版"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    :confirm-btn="null"
    width="80%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-table rowKey="id" :data="data" :columns="columns" hover :loading="dataLoading">
        <template #sort="{ rowIndex }">
          {{ rowIndex + 1 }}
        </template>
        <template #type="{ row }">
          {{ row.type | filterByDict(dict.type.msg_template_type) }}
        </template>
        <template #op="{ row }">
          <t-button
            @click="handleGroupSms(formData.id, row.id)"
            v-if="sendType === 1"
            size="small"
            variant="text"
            class="t-button-link"
          >
            <t-icon name="cloud" slot="icon" />
            群发
          </t-button>
          <t-button @click="handleSms(objId, row.id)" size="small" variant="text" class="t-button-link">
            <t-icon name="cloud" slot="icon" />
            {{ isJx ? '单发' : '生成短信' }}
          </t-button>
        </template>
      </t-table>
    </div>
  </t-dialog>
</template>

<script>
export default {
  name: 'expert-sms-template',
  dicts: ['msg_template_type'],
  props: {
    formData: {
      type: Object,
      default: () => ({}),
    },
    sendType: {
      type: Number,
    },
    objId: {
      type: Number,
    },
    nickName: {
      type: String,
    },
    isJx: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      visible: true,
      dataLoading: false,
      data: [],
      columns: [
        {
          colKey: 'sort',
          title: '序号',
          align: 'center',
          width: 64,
        },
        {
          colKey: 'type',
          title: '模版类型',
          align: 'center',
        },
        {
          colKey: 'content',
          title: '模版内容',
          align: 'center',
        },
        {
          colKey: 'op',
          title: '操作',
          align: 'center',
          width: 100,
        },
      ],
    };
  },
  created() {
    let type = '1';
    if (['2', '3'].includes(this.formData.reviewType)) {
      type = this.formData.reviewType;
    }
    this.getSmsUsedListByType(type);
  },
  methods: {
    getSmsUsedListByType(type) {
      this.$api.system.sms
        .getUsedListByType(type)
        .then((response) => {
          this.data = response.data;
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    handleGroupSms(teamId, tempId) {
      let loading = false;
      const confirmDia = this.$dialog.confirm({
        header: '发送短信提示（群发）',
        body: `是否确认为本组专家发送邀请短信？`,
        confirmBtn: {
          content: loading ? '提交中...' : '提交',
          theme: 'primary',
          loading,
        },
        theme: 'warning',
        onConfirm: ({ e }) => {
          loading = true;
          this.$api.kjxm.review
            .sendMsgByTeamIdAndTemplateId(teamId, tempId)
            .then((response) => {
              this.$message.success('发送成功');
              this.$emit('reload');
              this.$emit('update:visible', false);
              confirmDia.destroy();
              loading = false;
            })
            .catch((e) => {
              loading = false;
              this.$message.error(e.toString());
            });
        },
      });
    },
    handleSms(id, tempId) {
      if (!this.isJx) {
        this.$api.kjxm.review
          .createMsgByIdAndTemplateId(id, tempId)
          .then((response) => {
            this.$message.error({
              content: response.msg,
              placement: 'center',
              closeBtn: true,
              duration: 0,
              style: { backgroundColor: '#fef0f0', color: 'black' },
            });
            this.closeDialog()
          })
          .catch((e) => {
            this.$message.error(e.toString());
          });
      } else {
        let loading = false;
        const confirmDia = this.$dialog.confirm({
          header: '发送短信提示',
          body: `是否确认为专家"${this.nickName}"发送邀请短信？`,
          confirmBtn: {
            content: loading ? '提交中...' : '提交',
            theme: 'primary',
            loading,
          },
          theme: 'warning',
          onConfirm: ({ e }) => {
            loading = true;
            this.$api.kjxm.review
              .sendMsgByIdAndTemplateId(id, tempId)
              .then((response) => {
                this.$message.success('发送成功');
                this.$emit('reload');
                this.$emit('update:visible', false);
                confirmDia.destroy();
                loading = false;
              })
              .catch((e) => {
                loading = false;
                this.$message.error(e.toString());
              });
          },
        });
      }
    },
    closeDialog() {
      this.$emit('update:visible', false);
    },
  },
};
</script>

<style scoped>
</style>
